/**
 * 
 */
package cn.com.xf.utils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * 
 */
public class DateUtils {
	
	public static SimpleDateFormat DATE = new SimpleDateFormat("yyyy-MM-dd");
	public static SimpleDateFormat DATETIME = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	public static SimpleDateFormat DATETIMESECOND = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static SimpleDateFormat TIME = new SimpleDateFormat("HH:mm");
	public static SimpleDateFormat MONTH = new SimpleDateFormat("yyyy-MM");
	
	public static Date parseDatetimeSecond(String date) throws ParseException {
		return DATETIMESECOND.parse(date);
	}
	
	public static Date parseDate(String date) throws ParseException {
		return DATE.parse(date);
	}
	
	public static Date parseDatetime(String date) throws ParseException {
		return DATETIME.parse(date);
	}

	public static Date parseMonth(String date) throws ParseException {
		return MONTH.parse(date);
	}	
	
	public static String formatTime(Date date) {
		return TIME.format(date);
	}
	
	public static String formatDate(Date date) {
		return DATE.format(date);
	}
	
	public static String formatDatetime(Date date) {
		return DATETIME.format(date);
	}
	
	public static Date connectDateAndTime(Date date, String time) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat datetimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String dateString = dateFormat.format(date);
		String datetimeString = dateString + " " + time;
		return datetimeFormat.parse(datetimeString);
	}
	
	public static double getHour(Date start, Date end) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		if (start.compareTo(end) <= 0) {
			long diff = end.getTime() - start.getTime();
			long day = diff / nd;
			long hour = diff % nd / nh + day * 24;
			long min = diff % nd % nh / nm;
			double m = min / 60.0;
			double h = m + hour;
			BigDecimal bg = new BigDecimal(h);
			return bg.setScale(1,BigDecimal.ROUND_HALF_UP).doubleValue();
		}
		return -1;
	}
	
	public static Date getFirstDate(List<Date> list) {
		Date curDate = list.get(0);
		for (int i = 0; i < list.size(); i++) {
			if (curDate.compareTo(list.get(i)) >= 0) {
				curDate = list.get(i);
			}
		}
		return curDate;
	}
	
	public static Date getLastDate(List<Date> list) {
		Date curDate = list.get(0);
		for (int i = 0; i < list.size(); i++) {
			if (curDate.compareTo(list.get(i)) <= 0) {
				curDate = list.get(i);
			}
		}
		return curDate;
	}
	
	public static int getDay(Date fDate, Date oDate) {

	       Calendar aCalendar = Calendar.getInstance();

	       aCalendar.setTime(fDate);

	       int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

	       aCalendar.setTime(oDate);

	       int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

	       return day2 - day1;

	    }
	
	public static Date getBeginTimeOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}
	
	public static Date getEndTimeOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		return calendar.getTime();
	}
}
